package com.qingxin.wuye.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.entity.WyCity;
import com.qingxin.wuye.service.WyCityService;

@Controller
@RequestMapping("/city")
public class CityAction extends BaseAction {

	@Autowired
	private WyCityService cityService;
	
	
	/**
	 * 获取表单数据
	 * @param companyid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getCityData.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getCityData(String companyid, HttpServletRequest request){
		return cityService.getUITreeData(companyid, getLoginUser(request), false);
	}
	
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(SystemCfg.isAdminUser(loginUser)){
			return "wuye/city/city_list_admin";
		}
		return "wuye/city/city_list";
	}
	
	
	/**
	 * 获取列表数据
	 * @param datatype
	 * @param leftdata
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String datatype, String leftdata, PageParam pageParam, HttpServletRequest request){
		String companyid = "";
		if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			companyid = leftdata;
		}
		return cityService.list(companyid, pageParam, request);
	}
	
	
	/**
	 * 添加信息
	 * @param model
	 * @param datatype
	 * @param leftdata
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String datatype, String leftdata, HttpServletRequest request){
		String companyid = "";
		if(SystemCfg.UI_DATATYPE_COMPANY.equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			companyid = leftdata;
		}
		LoginUser loginUser = getLoginUser(request);
		if(!SystemCfg.isAdminUser(loginUser)){
			companyid = loginUser.getCompanyid();
		}
		model.put("companyid", companyid);
		
		return "wuye/city/city_add";
	}
	
	
	/**
	 * 保存
	 * @param city
	 * @param result
	 * @param companyid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid WyCity city, BindingResult result, String companyid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		city.setCompanyid(companyid);
		
		SysError info = cityService.add(city, getLoginUser(request), request);
		return info;
	}

	
	/**
	 * 修改信息
	 * @param model
	 * @param cityid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String cityid){
		WyCity city = cityService.get(cityid);
		model.put("city", city);
		
		return "wuye/city/city_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param city
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid WyCity city, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = cityService.update(city, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 删除
	 * @param cityid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] cityid, HttpServletRequest request){
		if(cityid==null || cityid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return cityService.delete(cityid, request);
	}
	
	
	/**
	 * 判断名称是否存在
	 * @param cityName
	 * @param cityid
	 * @param companyid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String cityName, String cityid, String companyid){
		boolean existed = cityService.isNameDuplicate(cityName, cityid, companyid);
		return existed ? "1":"0";
	}
	
	
}
